package com.alibaba.cloud.application.handler;

import com.alibaba.cloud.domain.dto.SmsPlaceHolderParameter;
import com.alibaba.cloud.domain.dto.SmsTemplateContext;
import com.alibaba.cloud.application.handler.template.TemplatePlaceHolderHandler;
import com.alibaba.cloud.application.sms.strategy.SmsSendRejectStrategy;
import com.alibaba.cloud.util.JacksonUtils;
import com.alibaba.cloud.util.PlaceHolderUtils;
import com.alibaba.cloud.util.SmsConstant;

/**
 * @author cruise
 * @description 默认的短信模板占位符处理器
 */
public class DefaultTemplatePlaceHolderHandler implements TemplatePlaceHolderHandler {

    private SmsSendRejectStrategy rejectStrategy;

    public DefaultTemplatePlaceHolderHandler(SmsSendRejectStrategy rejectStrategy) {
        this.rejectStrategy = rejectStrategy;
    }

    @Override
    public String handle(SmsTemplateContext templateContext, SmsPlaceHolderParameter parameter) {
        // 发送拒绝策略
        rejectStrategy.reject(templateContext, parameter);
        return PlaceHolderUtils.replacePlaceHolder(templateContext.getTemplateContent(), JacksonUtils.toMap(parameter),
                SmsConstant.DEFAULT_PLACE_HOLDER_REGEX, SmsConstant.DEFAULT_PLACE_HOLDER_KEY_REGEX);
    }
}
